package proj1;

import java.util.LinkedList;

public abstract class Problem {

	LinkedList<Node> allNodes; 
	LinkedList<String> operators;
	Node initialNode;
	public Problem(LinkedList<String> operators2){
		this.operators = operators2;
		this.allNodes=new LinkedList<Node>();
		
	}
	
	public abstract boolean goalTest(Node node);
	
	public abstract void pathCost(Node node);

}

/**
 * Class representing a tile on a grid
 * @author Draz
 *
 */
class Tile {
	private static int UID = 0;
	public int id, size, x, y;
	public boolean orientation;
	/**
	 * Create tile with unique id
	 */
	Tile(){
		id = UID++;
	}
	/**
	 * Creates a tile with requested id while making sure it's unique
	 * @param i
	 */
	Tile(int i){
		if(UID >= i) i = UID;
		id = i;
		UID = id+1;
	}
	/**
	 * Creates a Tile with specified values
	 * @param i id
	 * @param x vertical position
	 * @param y horizontal position
	 * @param s tile size
	 * @param o orientation
	 */
	Tile(int i, int x, int y, int s, boolean o){
		this(i);
		orientation = o;
		size = s;
		this.x = x;
		this.y = y;
	}
	/**
	 * Creates a new tile with identical values to another tile
	 * @param t the Tile to copy from
	 */
	Tile(Tile t){
		x = t.x;
		y = t.y;
		size = t.size;
		orientation = t.orientation;
		id = t.id;
	}
	/**
	 * Tries to insert a tile into a grid
	 * @param grid
	 * @return whether the tile was inserted or not
	 */
	public boolean insert(int[][] grid){
		orientation = (((int) Math.round(Math.random()))==1); //if true Vertical else Horizontal
		if (!orientation) { // horizontal tile
			size = 2;
			x = (int) Math.round(Math.random() * (grid.length - 1));
			y = (int) Math.round(Math.random() * (grid.length - size));
			if (x != 2 && grid[x][y] == -1 && grid[x][y + 1] == -1) {
				grid[x][y] = id;
				grid[x][y + 1] = id;
			} else {
				return false;
			}
		} else { // vertical tile
			size = (int) Math.round(Math.random()) + 2;
			x = (int) Math.round(Math.random() * (grid.length - size));
			y = (int) Math.round(Math.random() * (grid.length - 1));
			if (size == 2) {// tile of size 2
				if (grid[x][y] == -1 && grid[x + 1][y] == -1) {
					grid[x][y] = id;
					grid[x + 1][y] = id;
				} else {
					return false;
				}
			} else { // tile of size 3
				if (grid[x][y] == -1 && grid[x + 1][y] == -1
						&& grid[x + 2][y] == -1) {
					grid[x][y] = id;
					grid[x + 1][y] = id;
					grid[x + 2][y] = id;
				} else {
					return false;
				}
			}
			
		}
		
		return true;
	}

}

